<?php

declare(strict_types=1);

namespace App\Command;

use App\Model\XApiCompanyExtend;
use App\Model\ZaXApiCompanyExtend;
use App\Service\UserService;
use App\Services\CompanyAttendanceRedisCacheService;
use FooRedis;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Hyperf\Database\Model\ModelNotFoundException;
use Hyperf\Utils\Coroutine;
use Hyperf\Utils\Exception\ParallelExecutionException;
use Hyperf\Utils\Parallel;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;

/**
 * @Command
 */
class SyncCompanyExtend extends HyperfCommand
{


    private $container;
    public function __construct(ContainerInterface $container)
    {
        parent::__construct('sync:companyExtend');
        $this->container = $container;
    }

    public function configure()
    {
        parent::configure();
        $this->setDescription('sync');
    }

    /**
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function handle()
    {
        $this->line('Hello Hyperf!', 'info');

//        $service = $this->container->get(UserService::class);
//
//        $service->companyAttendanceRedisCache($date);


        $list = ZaXApiCompanyExtend::query()->orderBy("edit_time","asc")->get();
        $parallel = new Parallel(5);
        foreach ($list as $item){


            $parallel->add(function () use ($item) {
                $this->getMyCompanyExtendInfo($item);
                return Coroutine::id();
            });
        }


        try{
            $results = $parallel->wait();
//            var_dump($results);
        } catch(ParallelExecutionException $e){
            // $e->getResults() 获取协程中的返回值。
            // $e->getThrowables() 获取协程中出现的异常。
        }
        return $results;

    }

    private function getMyCompanyExtendInfo(ZaXApiCompanyExtend $zaXApiCompanyEphpxtend){
        try{
            $info = XApiCompanyExtend::query()->findOrFail($zaXApiCompanyExtend->id);
//            if ($info->edit_time == $zaXApiCompanyExtend->edit_time ){
//                var_dump($info->edit_time , $zaXApiCompanyExtend->edit_time);
                $info->score = $zaXApiCompanyExtend->score;
                $info->field1 = $zaXApiCompanyExtend->field1;
                $info->field2 = $zaXApiCompanyExtend->field2;
                $info->field3 = $zaXApiCompanyExtend->field3;
                $info->edit_time = $zaXApiCompanyExtend->edit_time;
                $info->save();
//            }else{
//
//            }
        }catch (ModelNotFoundException $exception){

        }
    }
    protected function getArguments()
    {
        return [
        ];
    }
}
